package zwp.example.demo1.demo_controller;

import com.example.common.utils.jwt.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import zwp.example.demo1.demo_mapper.DishMapper;
import zwp.example.demo1.demo_pojo.domain.DishDomain;
import zwp.example.demo1.demo_pojo.pojo.AuthRequest;
import zwp.example.demo1.demo_pojo.pojo.Result;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class AuthController {
    /**
     * 依赖业务层对象
     */
    @Autowired
    private DishMapper dishMapper;

    @PostMapping("/login")
    public Result login(@RequestBody AuthRequest emp) {
        //调用业务层：登录功能
        DishDomain loginEmp = dishMapper.queryDish();

        //判断：登录用户是否存在
        if (loginEmp != null) {
            //自定义信息
            Map<String, Object> claims = new HashMap<>();
//            claims.put("id", loginEmp.getId());
//            claims.put("username",loginEmp.getUsername());
            claims.put("name", loginEmp.getName());

            //使用JWT工具类，生成身份令牌
            String token = JwtUtils.generateJwt(claims);
            return Result.success(token);
        }
        return Result.error("用户名或密码错误");
    }
}